<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
              >
                新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="showAssignPerm(row.id)"
                  >
                    分配权限
                  </el-button>
                  <el-button size="small" type="primary" @click="editRole(row)">
                    编辑
                  </el-button>
                  <el-button size="small" type="danger" @click="delRole(row)">
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <!-- 放置分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <el-pagination
                :current-page="query.page"
                :page-size="query.pagesize"
                :total="query.total"
                :page-sizes="[2, 5, 10]"
                layout="prev, pager, next,sizes"
                @current-change="changePage"
                @size-change="handleSizeChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 对话框 -->
    <el-dialog
      :title="roleForm.id ? '编辑角色' : '添加角色'"
      :visible="showDialog"
      width="50%"
      :before-close="handleClose"
      @close="btnCancel"
    >
      <el-form ref="fm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOK">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分配权限对话框 -->
    <AssignPerm ref="ap" :show-assign-dialog.sync="showAssignDialog" />
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting.js'
import AssignPerm from './components/assign-perm'
export default {
  name: 'Setting',
  components: {
    AssignPerm
  },
  data () {
    return {
      // 分配权限的对话框显示与隐藏
      showAssignDialog: false,
      // 提示框的表单
      roleForm: {
        name: '',
        description: ''
      },
      // 提示框的表单效验
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }, { min: 2, max: 50, message: '角色名称长度必须2-50位', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }, { min: 2, max: 50, message: '角色描述长度必须2-50位', trigger: 'blur' }]
      },
      // 对话框显示与隐藏
      showDialog: false,
      list: [],
      query: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数
      }
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    // 页码变化
    handleSizeChange (size) {
      this.query.pagesize = size
      this.getDataList()
    },
    // 点击分配权限按钮
    showAssignPerm (id) {
      this.showAssignDialog = true
      this.$refs.ap.getAssignPerm(id)
    },
    // 点击编辑按钮
    async editRole (row) {
      const res = await getRoleDetail(row.id)
      this.roleForm = res
      this.showDialog = true
    },
    // 点击添加事件
    async btnOK () {
      try {
        await this.$refs.fm.validate()
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        this.showDialog = false
        this.getDataList()
        this.$message.success('操作成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 对话框关闭事件
    btnCancel () {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.fm.resetFields()
      this.showDialog = false
    },
    // 获取数据
    async getDataList () {
      const { rows, total } = await getRoleList(this.query)
      this.list = rows
      this.query.total = total
    },
    // 删除角色
    /**
     * role 当前删除角色数据
     */
    async delRole (role) {
      // console.log(role)
      /**
           * 思路：
           * 1. 用户确认
           * 2. 确认之后，调用接口删除
           * 3. 刷新列表
           * 4. 提示消息
           */
      try {
        await this.$confirm(`确认删除${role.name}这个角色吗`)
        //  点击确认
        await deleteRole(role.id)
        if (this.list.length === 1 && this.query.page > 1) {
          this.query.page--
        }
        this.getDataList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    changePage (page) {
      this.query.page = page
      // 刷新列表
      this.getDataList()
    },
    // 点击叉号关闭对话框
    handleClose () {
      this.showDialog = false
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
